<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch">
      <el-form-item label="订单号" prop="orderId">
        <el-input v-model="queryParams.orderId" placeholder="请输入订单号" clearable style="width: 200px"
          @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="物流单号" prop="orderPackageId">
        <el-input v-model="queryParams.orderPackageId" placeholder="请输入物流单号" clearable style="width: 200px"
          @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="收货人" prop="consignee">
        <el-input v-model="queryParams.consignee" placeholder="请输入收货人" clearable style="width: 200px"
          @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="手机号" prop="phone">
        <el-input v-model="queryParams.phone" placeholder="请输入手机号" clearable style="width: 200px"
          @keyup.enter="handleQuery" />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    

    <el-table v-loading="loading" :data="noticeList" @selection-change="handleSelectionChange">
      <el-table-column label="序号" align="center" type="index" width="100">
        <template #default="{ $index }">
          {{ (queryParams.currentPage - 1) * queryParams.pageSize + $index + 1 }}
        </template>
      </el-table-column>
      <el-table-column label="订单号" align="center" prop="orderId" />
      <el-table-column label="包裹详情" align="center" prop="packageDetail">
        <template #default="scope">
          <el-button link type="primary" icon="Eyes" @click="handleDuser(scope.row, 'packageDetail')">包裹详情</el-button>
        </template>
      </el-table-column>
      <el-table-column label="物流单号" align="center" prop="orderPackageId" />
      <el-table-column label="物流方式" align="center" prop="transportType" width="100">
        <template #default="scope">
          <dict-tag :options="transport_type" :value="scope.row.transportType" />
        </template>
      </el-table-column>
      <el-table-column label="物流详情" align="center" prop="logisticsDetail">
        <template #default="scope">
          <el-button link type="primary" icon="Eyes" @click="handleDuser(scope.row, 'logisticsDetail')">物流详情</el-button>
        </template>
      </el-table-column>
      <el-table-column label="收货人" align="center" prop="consignee" />
      <el-table-column label="手机号" align="center" prop="phone" />
      <el-table-column show-overflow-tooltip label="收货地址" align="center" prop="address" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="100">

      </el-table-column>
      <el-table-column label="状态" align="center" prop="status" >
        <template #default="scope">
          <!-- 1运输中 2已送达 -->
          <span v-if="scope.row.status == 1">运输中</span>
          <span v-if="scope.row.status == 2">已送达</span>
        </template>
      </el-table-column>
      <el-table-column label="操作人员" align="center" prop="operator" />
      <!-- <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
            <el-button
            link type="primary" icon="Delete" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column> -->
    </el-table>

    <pagination v-show="total > 0" :total="total" v-model:page="queryParams.currentPage"
      v-model:limit="queryParams.pageSize" @pagination="getList" />

    <!-- 添加或修改物流对话框 -->
    <el-dialog :title="title" v-model="open" width="780px" append-to-body>
      <el-table :data="gridData">



        <el-table-column v-if="!isPackageorDetail" property="address" label="地址" />
        <el-table-column v-if="!isPackageorDetail" property="createTime" label="创建时间" />



        <el-table-column v-if="isPackageorDetail" label="名称" align="center" prop="name" />
        <el-table-column v-if="isPackageorDetail" label="图片" align="center" prop="image">
          <template #default="scope">
            <el-image :src="scope.row.image" :preview-teleported="true" :width="100" :height="100" />
          </template>
        </el-table-column>
        <el-table-column v-if="isPackageorDetail" label="型号" align="center" prop="model" />

        <el-table-column v-if="isPackageorDetail" label="规格" align="center" prop="spec" />

        <el-table-column v-if="isPackageorDetail" label="价格" align="center" prop="price" />
        <el-table-column v-if="isPackageorDetail" label="数量" align="center" prop="count" />


      </el-table>
    </el-dialog>

  </div>
</template>

<script setup name="Notice">
import {
  listTransport,
  delTransport,
  produceApiTransportDetail,
  produceApiTransportPackageDetail,
  getNotice,

  addNotice,
  updateNotice,
} from "@/api/backProduce/backProduce";

const { proxy } = getCurrentInstance();
const { sys_notice_status, sys_notice_type, transport_type } = proxy.useDict(
  "sys_notice_status",
  "sys_notice_type",
  "transport_type"
);

const noticeList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    currentPage: 1,
    pageSize: 10,
    noticeTitle: undefined,
    createBy: undefined,
    status: undefined,
  },
  rules: {
    // noticeTitle: [
    //   { required: true, message: "物流标题不能为空", trigger: "blur" },
    // ],
    // noticeType: [
    //   { required: true, message: "物流类型不能为空", trigger: "change" },
    // ],
  },
});

const { queryParams, form, rules } = toRefs(data);


let isPackage = ref(false)

/** 预约用户列表 */
const gridData = ref([])
let isPackageorDetail = ref(false)
// 包裹详情
const handleDuser = async (row, type) => {
  if (type == 'packageDetail') {
    title.value = "包裹详情";
  }else{
    title.value = "物流信息列表";
  }
    open.value = true;
    let orderPackageId = row.orderPackageId
    let data = await produceApiTransportPackageDetail(orderPackageId)
    let detail = await produceApiTransportDetail(orderPackageId)
    if (type == 'packageDetail') {
    gridData.value = [...data]
    isPackageorDetail.value = true
  } else {
    gridData.value = [...detail]
    isPackageorDetail.value = false
  }
}


/** 查询物流列表 */
function getList() {
  loading.value = true;
  listTransport(queryParams.value).then((response) => {
    noticeList.value = response.records;
    total.value = response.total;
    loading.value = false;
  });
}

/** 取消按钮 */
function cancel() {
  open.value = false;
  reset();
}

/** 表单重置 */
function reset() {
  form.value = {
    orderPackageId: undefined,
    noticeTitle: undefined,
    noticeType: undefined,
    noticeContent: undefined,
    status: "0",
  };
  proxy.resetForm("noticeRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.currentPage = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

/** 多选框选中数据 */
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.orderPackageId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加物流";
}

/**修改按钮操作 */
function handleUpdate(row) {
  reset();
  const orderPackageId = row.orderPackageId || ids.value;
  getNotice(orderPackageId).then((response) => {
    form.value = response;
    open.value = true;
    title.value = "修改物流";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["noticeRef"].validate((valid) => {
    if (valid) {
      if (form.value.orderPackageId != undefined) {
        updateNotice(form.value).then((response) => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addNotice(form.value).then((response) => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const orderPackageIds = row.orderPackageId || ids.value;
  proxy.$modal
    .confirm('是否确认删除物流编号为"' + orderPackageIds + '"的数据项？')
    .then(function () {
      return delTransport(orderPackageIds);
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => { });
}

getList();
</script>
